package com.dream.libxrec.cut;

import android.media.MediaFormat;
import android.text.TextUtils;
import android.util.Log;
import com.dream.libxrec.player.audio.AudioDecoder;
import com.dream.libxrec.player.audio.IAudioDecodeCallback;
import com.dream.libxrec.player.video.VideoDecoder;
import java.io.File;
import java.util.List;
import java.util.Objects;

public class MergeUtils {
    private static final String TAG = "MergeUtils";

    public boolean merge(List<File> list,String destFile){
        if (!isSupport(list)){
            return false;
        }

        for (File f:list){
            boolean isEncodeVideo = false;
            boolean isEncodeAudio = false;

            String path = f.getAbsolutePath();
            long pts = 0;

            new Thread(()->{

                try {
                    AudioDecoder audioDecoder = new AudioDecoder();
                    boolean isA = audioDecoder.init(path, new IAudioDecodeCallback() {
                        @Override
                        public void init(MediaFormat format) {}

                        @Override
                        public void onDecode(byte[] data, int size, long pts) {

                        }

                        @Override
                        public void onFinish() {

                        }
                    });
                    if (isA){
                        audioDecoder.isEndRelease(true);
                        audioDecoder.start();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }).start();
        }

        return true;
    }

    /**
     * 文件是否支持剪辑
     * @param list 文件
     * @return
     */
    public boolean isSupport(List<File> list) {

        if (list == null||list.isEmpty()){
            Log.e(TAG,"剪辑的文件列表为空！");
            return false;
        }

        String audioType = "";
        String videoType = "";

        for (File f:list){
            if (f == null||!f.exists()||!f.isFile()){
                Log.e(TAG,"文件异常！"+(f!=null?f.getAbsolutePath():""));
                return false;
            }

            AudioDecoder audio = new AudioDecoder();
            try {
                boolean isAudioCheck = new AudioDecoder().init(f.getAbsolutePath(),null);
                if (!isAudioCheck){
                    Log.e(TAG,"未获取到音频轨！");
                    //TODO:无音频轨如果視頻正常其实不影响合并，但暂时不处理
                    return false;
                }

                String type = audio.getFormat().getString(MediaFormat.KEY_MIME);

                if (TextUtils.isEmpty(audioType)){
                    audioType = type;
                }else if (!Objects.equals(type, audioType)){
                    Log.e(TAG,"不支持不同音频格式的文件合并剪辑！");
                    return false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG,"解码文件失败！"+f.getAbsolutePath());
                return false;
            }

            try {
                boolean isVideoCheck = new VideoDecoder().init(f.getAbsolutePath(),null,null);
                if (!isVideoCheck){
                    Log.e(TAG,"未获取到视频轨！");
                    //TODO:无视频轨如果音频正常，其实不影响合并，但暂时不处理
                    return false;
                }

                String type = audio.getFormat().getString(MediaFormat.KEY_MIME);

                if (TextUtils.isEmpty(videoType)){
                    videoType = type;
                }else if (!Objects.equals(type, videoType)){
                    Log.e(TAG,"不支持不同視频格式的文件合并剪辑！");
                    return false;
                }
            }catch(Exception e){
                e.printStackTrace();
                Log.e(TAG,"解码文件失败！"+f.getAbsolutePath());
                return false;
            }
        }

        return true;
    }
}
